package com.example.smarterhouse;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;
import android.widget.Toast;

public class ExpandableListAdapter extends BaseExpandableListAdapter {
	


	  private final SparseArray<Group> groups;
	  public LayoutInflater inflater;
	  public Activity activity;
	  public Context con;
	  private Controller controller;
	  private List<List<String>> listList;

	  public ExpandableListAdapter(Activity act, SparseArray<Group> groups, Context c, List<List<String>> list) {
	    activity = act;
	    this.groups = groups;
	    inflater = act.getLayoutInflater();
	    this.con = c;
	    controller = Controller.getInstance();
	    this.listList = list;
	    
	  }

	  @Override
	  public Object getChild(int groupPosition, int childPosition) {
	    return groups.get(groupPosition).children.get(childPosition);
	  }

	  @Override
	  public long getChildId(int groupPosition, int childPosition) {
	    return 0;
	  }

	  @Override
	  public View getChildView(final int groupPosition, final int childPosition,
	      boolean isLastChild, View convertView, ViewGroup parent) {
	    final String children = (String) getChild(groupPosition, childPosition);
	    TextView text = null;
	    if (convertView == null) {
	      convertView = inflater.inflate(R.layout.listrow_details, null);
	    }
	    text = (TextView) convertView.findViewById(R.id.textView1);
	    text.setText(children);
	    convertView.setOnClickListener(new OnClickListener() {
	      @Override
	      public void onClick(View v) {
	    	  Log.d("Group position", String.valueOf(groupPosition));
	    	  Log.d("Child position", String.valueOf(childPosition));
	    	  
	    	  String nr = "getDevice_" + listList.get(groupPosition).get(childPosition) + "_";
	  		Log.d("RoomClass", nr);
	  		// controll.setStringToServer(nr);
	  		controller.setCon(con);
	  		controller.setClassDest(Functions.class);

	  		// **********************************************
	  		// Added By Sanja & Henrik for the async call
	  		// **********************************************

	  		if (!controller.callAsync(nr)) {
	  			Toast toast = Toast.makeText(con,
	  					"Failed to get status for " + children
	  							+ " please try again", Toast.LENGTH_LONG);
	  			toast.show();
	  		}
	    	  
	        Toast.makeText(activity, children,
	            Toast.LENGTH_SHORT).show();
	      }
	    });
	    return convertView;
	  }

	  @Override
	  public int getChildrenCount(int groupPosition) {
	    return groups.get(groupPosition).children.size();
	  }

	  @Override
	  public Object getGroup(int groupPosition) {
	    return groups.get(groupPosition);
	  }

	  @Override
	  public int getGroupCount() {
	    return groups.size();
	  }

	  @Override
	  public void onGroupCollapsed(int groupPosition) {
	    super.onGroupCollapsed(groupPosition);
	  }

	  @Override
	  public void onGroupExpanded(int groupPosition) {
	    super.onGroupExpanded(groupPosition);
	  }

	  @Override
	  public long getGroupId(int groupPosition) {
	    return 0;
	  }

	  @Override
	  public View getGroupView(int groupPosition, boolean isExpanded,
			  
			  
	      View convertView, ViewGroup parent) {
	    if (convertView == null) {
	      convertView = inflater.inflate(R.layout.listrow_group, null);
	    }
	    Group group = (Group) getGroup(groupPosition);
	    ((CheckedTextView) convertView).setText(group.string);
	    ((CheckedTextView) convertView).setChecked(isExpanded);
	    return convertView;
	    
	  }

	  @Override
	  public boolean hasStableIds() {
	    return false;
	  }

	  @Override
	  public boolean isChildSelectable(int groupPosition, int childPosition) {
	    return false;
	  }
	} 